package com.chen.form;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <b>Servlet对Check表单数据的获取</b>
 * <p>
 * 描述:<br>
 * 使用getParameterNames()获取所有表单名称枚举对象，<br>
 * 在通过hasMoreElements()判断是否存在下一个，使用nextElement()来获取当前表单对象<br>
 * 以控制台的方式输出结果
 * @author 威 
 * <br>2018年5月22日 下午7:27:04 
 * @see
 * @since 1.0
 */
@WebServlet("/CheckForm")
public class CheckForm extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public CheckForm() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		@SuppressWarnings("rawtypes")
		Enumeration paramNames = request.getParameterNames();
		
		while(paramNames.hasMoreElements()){
			String paramName = (String) paramNames.nextElement();
			System.out.print(paramName);
			String[] paramValues = request.getParameterValues(paramName);
			if(paramValues.length == 1){
				//只有一个值
				String paramValue = paramValues[0];
				System.out.print("-" + (paramValue.length() == 0 ? "没有值" : paramValue));
			}
			else {
				//读取多个值
				for(int i = 0; i < paramValues.length; i++)
					System.out.print(paramValues[i]);
			}
			System.out.println();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
					throws ServletException, IOException {
		doGet(request, response);
	}

}
